package cn.xinfei.xdecision.common.dynamic;

public class DynamicDatasourceContextHolder {
    private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<>();

    public static void setDataSourceType(DataSourceType type) {
        if (type == null) {
            throw new NullPointerException();
        }

        contextHolder.set(type);
    }

    public static DataSourceType getDataSourceType() {
        DataSourceType type = contextHolder.get();
        if (type == null) {
            //确定一个默认数据源
            return DataSourceType.data;
        }
        return type;
    }

    public static void clearDataSourceType() {
        contextHolder.remove();
    }
}
